package com.handinfo.android.uicontrols.controls;

import android.graphics.Bitmap;
import android.graphics.PointF;

import com.handinfo.android.core.graphics.DWGraphics;
import com.handinfo.android.uicontrols.DWControlsManager;
import com.handinfo.android.uicontrols.DWExternalDraw;
import com.handinfo.android.utils.Tools;

/**
 * 图片容器
 * @author handinfo
 */
public class DWImageBox extends DWControl
{
	private boolean m_is_flicker = false;//是否闪烁
	private int m_flicker_no = 0;//不闪烁时间
	private int m_flicker_all = 0;//总时间
	
	private boolean m_auto_fill = true;//需不需要自动填充
	
	private boolean m_is_select = false;
	
	private int m_color = Tools.ALPHA;//图片没有显示区大时填充的颜色
	
	private DWExternalDraw m_external_draw = null;
	
	public static final byte SELECT_TYPE_RECT = 0;
	public static final byte SELECT_TYPE_ARC1 = 1;
	public static final byte SELECT_TYPE_ARC2 = 2;
	public static final byte SELECT_TYPE_ARC3 = 3;
	private byte m_select_type = SELECT_TYPE_RECT;
	
	public DWImageBox(Bitmap image)
	{
		this(image, 0, 0);
	}
	
	public DWImageBox(Bitmap image, int x, int y) {
		if (image == null) {
			Tools.debugPrintln("DWImageBox image is null!");
		}
		img_background = image;
		setPoint(x, y);
		setShowWideHigh(img_background.getWidth(), img_background.getHeight());
	}
	
	public DWImageBox(int w, int h) {
		setShowWideHigh(w, h);
	}
	
	public void setImage(Bitmap image) {
		if (image == null) {
			Tools.debugPrintln("DWImageBox setImage is null!");
		}
		img_background = image;
	}

	public void refreshControl()
	{
		
	}
	/**
	 * 添加一个外部绘制
	 * @param ed
	 */
	public void addExternalDraw(DWExternalDraw ed) {
		m_external_draw = ed;
	}
	/**
	 * 设置颜色
	 * @param color
	 */
	public void setColor(int color) {
		m_color = color;
	}
	/**
	 * 设置需不需要图片自动填充
	 * @param result
	 */
	public void setAutoFill(boolean result) {
		m_auto_fill = result;
	}
	/**
	 * 设置选择状态
	 * @param select
	 */
	public void setSelect(boolean select) {
		m_is_select = select;
	}
	/**
	 * 设置是否闪烁
	 * @param isFlicker 不闪烁为false
	 */
	public void setFlicker(boolean isFlicker) {
		m_is_flicker = isFlicker;
		setShow(true);
	}
	
	/**
	 * 设置闪烁比率
	 * @param noTime 不闪烁的时间
	 * @param allTime 总循环时间
	 */
	public void setFlickerRate(int noTime, int allTime) {
		m_flicker_no = noTime;
		m_flicker_all = allTime;
		setFlicker(true);
	}
	
	/**
	 * 设置选择类型
	 * @param type
	 */
	public void setSelectType(byte type) {
		m_select_type = type;
	}
	private int m_offset_w = 0;
	private int m_offset_h = 0;
	public void render(DWGraphics g)
	{
		if (m_is_show) {
			setClip(g);
			if (img_background != null) {
				if (m_auto_fill) {
					g.drawBitmap(img_background, m_show_x, m_show_y, m_show_w, m_show_h, Tools.LEFT_TOP);
				} else {
					g.setColor(m_color);
					g.fillRect(m_show_x, m_show_y, m_show_w, m_show_h);
					g.drawBitmap(img_background, m_show_x - m_offset_w, m_show_y - m_offset_h, Tools.LEFT_TOP);
				}
			}
			if (m_is_select) {
				if (m_select_type == SELECT_TYPE_RECT) {
					DWControlsManager.getInstance().paintSelectRect(g, m_show_x, m_show_y, m_show_w, m_show_h);
				} else if (m_select_type == SELECT_TYPE_ARC1) {
					DWControlsManager.getInstance().paintSelectArc1(g, m_show_x, m_show_y, m_show_w, m_show_h);
				} else if (m_select_type == SELECT_TYPE_ARC2) {
					DWControlsManager.getInstance().paintSelectArc2(g, m_show_x, m_show_y, m_show_w, m_show_h);
				} else if (m_select_type == SELECT_TYPE_ARC3) {
					DWControlsManager.getInstance().paintSelectArc3(g, m_show_x, m_show_y, m_show_w, m_show_h);
				}
			}
			
			if (m_external_draw != null) {
				m_external_draw.externalDraw(g, m_show_x - m_offset_w, m_show_y - m_offset_h);
			}
			renderRect(g);
			renderGuide(g);
		}
	}

	public void renderLimits(DWGraphics g, int x, int y, int w, int h, int offsetx, int offsety)
	{
		if (m_is_show) {
			setSubClip(g, x, y, w, h, offsetx, offsety);
			if (img_background != null) {
				if (m_auto_fill) {
					g.drawBitmap(img_background, x + m_show_x - offsetx, y + m_show_y - offsety, m_show_w, m_show_h, Tools.LEFT_TOP);
				} else {
					g.setColor(m_color);
					g.fillRect(m_show_x, m_show_y, m_show_w, m_show_h);
					g.drawBitmap(img_background, x + m_show_x - offsetx - m_offset_w, y + m_show_y - offsety - m_offset_h, Tools.LEFT_TOP);
				}
			}
			if (m_is_select) {
				if (m_select_type == SELECT_TYPE_RECT) {
					DWControlsManager.getInstance().paintSelectRect(g, x + m_show_x - offsetx, y + m_show_y - offsety, m_show_w, m_show_h);
				} else if (m_select_type == SELECT_TYPE_ARC1) {
					DWControlsManager.getInstance().paintSelectArc1(g, x + m_show_x - offsetx, y + m_show_y - offsety, m_show_w, m_show_h);
				} else if (m_select_type == SELECT_TYPE_ARC2) {
					DWControlsManager.getInstance().paintSelectArc2(g, x + m_show_x - offsetx, y + m_show_y - offsety, m_show_w, m_show_h);
				} else if (m_select_type == SELECT_TYPE_ARC3) {
					DWControlsManager.getInstance().paintSelectArc3(g, x + m_show_x - offsetx, y + m_show_y - offsety, m_show_w, m_show_h);
				}
			}
			if (m_external_draw != null) {
				m_external_draw.externalDraw(g, x + m_show_x - offsetx - m_offset_w, y + m_show_y - offsety - m_offset_h);
			}
			renderLimitsRect(g, x, y, offsetx, offsety);
			renderGuide(g, x - offsetx - m_offset_w, y - offsety - m_offset_h);
		}
	}
	private int m_timer = 0;
	public void logic()
	{
		if (img_background != null) {
			if (m_show_w > img_background.getWidth()) {
				m_offset_w = (int)(m_deviant_x - (m_show_w - img_background.getWidth()) / 2);
			} else {
				m_offset_w = (int)m_deviant_x;
			}
			if (m_show_h > img_background.getHeight()) {
				m_offset_h = (int)(m_deviant_y - (m_show_h - img_background.getHeight()) / 2);
			} else {
				m_offset_h = (int)m_deviant_y;
			}
		}
		if (m_is_flicker) {
			if (m_timer > m_flicker_all) {
				m_timer = 0;
			}
			if (m_timer <= m_flicker_no) {
				m_is_show = true;
			} else {
				m_is_show = false;
			}
			m_timer++;
		}
	}
	
	/**
	 * 设置图片位移
	 * @param x
	 * @param y
	 */
	public void setImageShift(float x, float y) {
		x -= m_show_w / 2;
		y -= m_show_h / 2;
		m_deviant_x = x;
		m_deviant_y = y;
		if (m_deviant_x < 0) {
			m_deviant_x = 0;
		} else if (m_deviant_x + m_show_w > m_actual_w) {
			m_deviant_x = m_actual_w - m_show_w;
		}
		if (m_deviant_y < 0) {
			m_deviant_y = 0;
		} else if (m_deviant_y + m_show_h > m_actual_h) {
			m_deviant_y = m_actual_h - m_show_h;
		}
		if (img_background != null) {
			if (m_show_w > img_background.getWidth()) {
				m_deviant_x = 0;
			}
			if (m_show_h > img_background.getHeight()) {
				m_deviant_y = 0;
			}
		}
	}
	
	public boolean doScroll(PointF start, PointF end, float distanceX, float distanceY)
	{
		if (checkPointOnRect(start.x, start.y)) {
			if (m_actual_h > m_show_h) {
				float a = m_actual_h / m_show_h;
				m_deviant_y += distanceY / a;
				if (m_deviant_y < 0) {
					m_deviant_y = 0;
				} else if (m_deviant_y + m_show_h > m_actual_h) {
					m_deviant_y = m_actual_h - m_show_h;
				}
			}
			if (m_actual_w > m_show_w) {
				float a = m_actual_w / m_show_w;
				m_deviant_x += distanceX / a;
				if (m_deviant_x < 0) {
					m_deviant_x = 0;
				} else if (m_deviant_x + m_show_w > m_actual_w) {
					m_deviant_x = m_actual_w - m_show_w;
				}
			}
		}
		return false;
	}
	public float m_click_x = 0;
	public float m_click_y = 0;
	
	/**
	 * 得到点击图片的坐标
	 * @return
	 */
	public PointF getClickPoint() {
		return new PointF(m_click_x, m_click_y);
	}
	
	/**
	 * 得到点击图片的x
	 * @return
	 */
	public float getClickX() {
		return m_click_x;
	}
	
	/**
	 * 得到点击图片的y
	 * @return
	 */
	public float getClickY() {
		return m_click_y;
	}
	
//	private boolean m_is_down = false;
//	public boolean doDown(PointF point) {
//		if (checkPointOnRect(point.x, point.y)) {
//			m_is_down = true;
//			return true;
//		}
//		return false;
//	}
	
//	public boolean doFling(PointF start, PointF end, float velocityX, float velocityY) {
//		if (m_is_down && checkPointOnRect(end.x, end.y)) {
//			if (m_listener != null) {
//				m_click_x = end.x - (m_show_x - m_deviant_x);
//				m_click_y = end.y - (m_show_y - m_deviant_y);
//				m_listener.OnClick();
//			}
//			setGuide(false);
//			return true;
//		}
//		return false;
//	}
//	
//	public boolean doDrag(PointF start, PointF end, float velocityX, float velocityY) {
//		if (m_is_down && checkPointOnRect(end.x, end.y)) {
//			if (m_listener != null) {
//				m_click_x = end.x - (m_show_x - m_deviant_x);
//				m_click_y = end.y - (m_show_y - m_deviant_y);
//				m_listener.OnClick();
//			}
//			setGuide(false);
//			return true;
//		}
//		return false;
//	}
	
	public boolean doClick(float x, float y)
	{
		if (checkPointOnRect(x, y)) {
			if (m_listener != null) {
				m_click_x = x - (m_show_x - m_deviant_x);
				m_click_y = y - (m_show_y - m_deviant_y);
				m_listener.OnClick();
			}
//			m_is_down = false;
			setGuide(false);
			return true;
		}
		return false;
	}

}
